/**
 * Statistic.java [V 1.0.0]
 * classes : com.xiaocq.iwordkiller.Statistic
 * xiaocq Create at 2012-4-20 下午8:57:42
 */
package com.xiaocq.iwordkiller;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * com.xiaocq.iwordkiller.Statistic
 * @author Mystery
 * Create at 2012-4-20 下午8:57:42
 */
public class Statistic extends Activity{
	private static final int CONFIRM_DIALOG = 1;
	
	private Param.StatisticParam sParam;
	
	private TextView showAllTestCounts, showAllRightCounts, showAllWrongCounts;
	private TextView showAllHitRate;
	private TextView showAllTimeCost;
	
	private Button backToMenu,clear;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.statistic);
		
		 sParam = new Param.StatisticParam(this);
		 sParam.load();
		 
		 showAllTestCounts = (TextView) findViewById(R.id.statistic_allTestCounts);
		 showAllRightCounts = (TextView) findViewById(R.id.statistic_allRightCounts);
		 showAllWrongCounts = (TextView) findViewById(R.id.statistic_allWrongCounts);
		 showAllHitRate = (TextView) findViewById(R.id.statistic_allHitRate);
		 showAllTimeCost = (TextView) findViewById(R.id.statistic_allTimeCost);
		 
		 setComponentsValue(); 
		 
		 backToMenu = (Button) findViewById(R.id.statistic_backToMenu);
		 backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		 
		 clear = (Button) findViewById(R.id.statistic_clear);
		 clear.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				showDialog(CONFIRM_DIALOG);
			}
		});
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		if(id == CONFIRM_DIALOG){
			builder.setMessage(R.string.tip_clearData);
			builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					sParam.clear();
					setComponentsValue();
				}
			});
			builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
				}
			});
			return builder.create();
		}
		return super.onCreateDialog(id);
	}
	
	private String formatTime(long timeInMillis){
		String result = "";
		long second,minute,hour,day;
		second = timeInMillis / 1000L;
		minute = second / 60L;
		second %= 60L;
		hour = minute / 60L;
		minute %= 60L;
		day = hour / 24L;
		hour %= 24L;
		
		if(day != 0) result += (day + "天");
		if(result.length() != 0){
			result += (hour + "时");
		}else if(hour != 0){
			result += (hour + "时");
		}
		
		if(result.length() != 0){
			result += (minute + "分");
		}else if(minute != 0){
			result += (minute + "分");
		}
		result += (second + "秒");
		
		return result;
	}
	
	private void setComponentsValue(){
		showAllTestCounts.setText("总测试：" + sParam.totalWords + "个");
		showAllRightCounts.setText("总答对：" + sParam.totalRight + "个");
		showAllWrongCounts.setText("总答错：" + sParam.totalWrong + "个");
		showAllHitRate.setText("正确率：" + String.format("%1$-4.2f", sParam.totalHitRate * 100) + "%");
		showAllTimeCost.setText(formatTime(sParam.totalTimeCost));
	}
}
